package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.model.db.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @auther : 白月尘
 * @date : 10:17  2021/10/24
 */
@DubboService
public class UserLocationApiImpl implements UserLocationApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    //保存地理位置
    @Override
    public void save(double latitude, double longitude, String addr, long userId) {
        //查询是否存在
        Criteria criteria = Criteria.where("userId").is(userId);
        boolean exists = mongoTemplate.exists(Query.query(criteria), UserLocation.class);
        UserLocation userLocation = new UserLocation();
        if (!exists) {
            //不存在则保存
            userLocation.setAddress(addr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUserId(userId);
            userLocation.setLastUpdated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude);
            userLocation.setLocation(geoJsonPoint);
            mongoTemplate.save(userLocation);
        } else {
            //如果存在则更新
            Query query = new Query(criteria);
            Update update = new Update();
            update.set("location", new GeoJsonPoint(longitude, latitude));
            update.set("address", addr);
            mongoTemplate.updateFirst(query, update, UserLocation.class);
        }
    }

    @Override
    public List<Long> findSearch(long userId, String distance) {
        Criteria criteria = Criteria.where("userId").is(userId);
        UserLocation userLocation = mongoTemplate.findOne(Query.query(criteria), UserLocation.class);
        // Point point=new Point(userLocation.getLocation().getX(), userLocation.getLocation().getY());
        GeoJsonPoint point = userLocation.getLocation();
        Distance distance1 = new Distance(Long.parseLong(distance) / 1000, Metrics.KILOMETERS);
        Circle circle = new Circle(point, distance1);
        Criteria criteria1 = Criteria.where("location").withinSphere(circle);
        Query query = Query.query(criteria1);
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);
        if (userLocations == null || userLocations.size() <= 0) {
            return new ArrayList<>();
        }
        //取出userId集合
        List<Long> longList = CollUtil.getFieldValues(userLocations, "userId", long.class);
        return longList;
    }
    // 自建后台查询坐标
    @Override
    public String selectLocation(Long userId) {

        System.err.println(userId);

        // 指定查询条件
        Query query = Query.query(Criteria.where("userId").is(userId));

        // 查询
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        // 获取经度
        double longitude = 0l;
        double latitude = 0l;
        // 获取坐标
        GeoJsonPoint location = userLocation.getLocation();
        if (location == null) {
            longitude = 116.635672;
            latitude = 40.169419;
        }else {
            longitude = location.getX();
            latitude = location.getY();
        }

        // 获取地址
        String address = userLocation.getAddress();

        // 获取经度
        //double longitude = location.getX();
        //double latitude = location.getY();


        // 构造map
        Map map = new HashMap();
        map.put("longitude",longitude);
        map.put("latitude",latitude);
        map.put("address",address);

        // 转换为json
        String locatonStr = JSON.toJSONString(map);


        return locatonStr;
    }
}
